package com.apes.fa.service;

import com.apes.fa.model.FaCard;
import com.apes.fa.model.FaCardAccount;
import com.apes.fa.respository.FaCardRepository;
import com.apes.framework.api.DomainService;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.masterdata.frame.repository.DeptRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Objects;

/**
 * @BelongsProject: apes-server
 * @BelongsPackage: com.apes.fa.service
 * @ClassName: FaCardService
 * @Author: PengZiFeng
 * @Description: 资产建卡服务
 * @CreateTime: 2021-08-23 11:47
 * @Version: 1.0
 */
@Service("faCardService")
public class FaCardService extends DomainService {

    @Autowired
    private FaCardRepository faCardRepository;
    @Autowired
    private FaCardAccountService faCardAccountService;
    @Autowired
    private DeptRepository deptRepository;

    /**
     * 保存
     */
    public FaCard save(SimpleRequest request){

        FaCard faCard = request.getO(FaCard.class);
        faCard.setState("sent");
        faCard.setBusinessName("(KP0)资产卡片建卡");
//        faCard.setBusinessName("(KP0)初期导入");
//        faCard = faCardAccountService.depreciationExpression(faCard);
//        faCard.setDept(faCard.getDepreciationDept());
        if (Objects.isNull(faCard.getUseDept())) faCard.setUseDept(faCard.getDept());
        //剩余折旧月
        if(faCard.isDepreciationMark() && "固定资产".equals(faCard.getFaType())) {
            faCard = faCardAccountService.depreciationExpression(faCard);
//            faCard.setDept(faCard.getDepreciationDept());
            faCard.setRemainingDepreciationMonth(faCard.getUseYear() * 12 + faCard.getUseMonth());
        }else if(faCard.isDepreciationMark() && !("固定资产".equals(faCard.getFaType()))){
            throw new RuntimeException("非固定资产不可提取折旧");
        }
        return faCardRepository.saveAndFlush(faCard);

    }

    /**
     * 删除
     */
    public String delete(SimpleRequest request){

        FaCard faCard = request.getO(FaCard.class);
        faCardRepository.delete(faCard);
        return "删除成功！";

    }

    /**
     * 修改
     */
    public FaCard update(SimpleRequest request){

        FaCard faCard = request.getO(FaCard.class);
        return faCardRepository.saveAndFlush(faCard);

    }

    /**
     * 审核
     */
    public FaCard approve(SimpleRequest request){

        String id = request.get("id");
        FaCard faCard = faCardRepository.findById(id).get();
        faCard.setState("approve");
        faCard.setApproveDate(new Date());
        faCard.setApproveUid(this.getPerson(request));
        return beforeApprove(faCard);

    }

    public FaCard beforeSave(FaCard faCard){
        faCard.setState("sent");
        if(faCard.isDepreciationMark() && "固定资产".equals(faCard.getFaType())) {
            faCard = faCardAccountService.depreciationExpression(faCard);
        }
        faCard = faCardRepository.saveAndFlush(faCard);
        return beforeApprove(faCard);

    }
    public FaCard beforeSaveNoAccount(FaCard faCard){
        faCard.setState("approve");
        faCard.setDepreciationMark(false);
        // faCard = faCardAccountService.depreciationExpression(faCard);
        return faCardRepository.saveAndFlush(faCard);

    }

    /**
     * 审核生成卡片账
     */
    public FaCard beforeApprove(FaCard faCard){

        if(!("固定资产".equals(faCard.getFaType()))){
            faCard.setState("approve");
        }else {
            faCard.setState("approve");
            FaCardAccount faCardAccount = new FaCardAccount();
            faCardAccount.setFaCardId(faCard.getId());
            faCardAccount.setSummary("1");
            faCardAccount.setQuantity(faCard.getQuantity());
            faCardAccount.setOriginalValueIncreased(faCard.getOriginalValue());
            faCardAccount.setOriginalValueDecrease(0.0);
            faCardAccount.setOriginalValueBalance(faCard.getOriginalValue());
            faCardAccount.setIncreaseDepreciation(0.0);
            faCardAccount.setReduceDepreciation(0.0);
            faCardAccount.setAccumulatedDepreciation(0.0);
            faCardAccount.setNetWorth(faCard.getOriginalValue());
            faCardAccount.setBookkeeper(faCard.getCustodian());
            faCardAccount.setBookkeepingDate(new Date());
            faCardAccount.setCompany(faCard.getCompany());
            Dept dept = deptRepository.findOne(faCard.getDept().getId());
            faCardAccount.setCostCenter(dept.getCostCenterId());
            faCardAccountService.create(faCardAccount);
        }
        return faCardRepository.saveAndFlush(faCard);

    }

    /**
     * 根据id查可资产减少的卡片
     */
    public FaCard findById(SimpleRequest request){
        FaCard faCard;
        String id = request.get("id");
        String businessType = request.get("businessType");
        String deptId =request.get("deptId");
        if("固定资产".equals(businessType)){
            faCard= faCardRepository.findByIdAndBusinessState(id,deptId);
        }else if("材料物资".equals(businessType)){
            throw new RuntimeException("材料物资无法报废： " + id);
        }
        else{
            faCard= faCardRepository.findByIdAndBusinessStateDZ(id,deptId);
        }
        return faCard;

    }

    /**
     * 根据id查可资产减少的卡片
     */
    public FaCard findChangeById(SimpleRequest request){

        String id = request.get("id");
        String deptId =request.get("deptId");
        FaCard faCard = faCardRepository.findChangeById(id,deptId);
        return faCard;

    }
}
